<?

/******************************************************************************
 ***
 *** PHP Sub Class Document
 *** Name:	System::Indexing.inc
 *** Owner:	eMobile Communications, LLC (Enrique A. Reyes-Otero)
 *** Developer:	Enrique A. Reyes-Otero
 *** Support:	support@emobilecommunications.com
 *** Copyright:	2007 © PHP System API
 *** Licensing: This document is governed by the eMobile Communications System
 ***		License found at in the System directory parallel to
 ***		this document.
 *** Legal:	eMobileCommunications.COM is the prorietor owner of this
 ***		document and the contents there in. If you have not been
 ***		authorized access to this document or its functionality then
 ***		your are advised to remove this document from your possesion.
 ***		Modification of this document without eMobileCommunications.COM
 ***		legal consent or any illegal or misuse of this document or the
 ***		contents and the functionality therein shall be prosecuted to
 ***		the full extent of the law.
 ***
 ******************************************************************************/

class Indexing {

	var $offset=0;
	var $limit=10;
	var $page=1;
	var $pages=1;
	var $prev=false;
	var $next=false;
	var $count=0;
	var $total=0;
	var $cols;
	var $order;
	var $db;

	/* instantiate class */
	function &Indexing(&$db,$query,$rows=0) {

		$this->db=$db;
		$this->rows=&$db->query($query);
		$query=$db->fetch_row($this->rows);
		if ($rows) $this->count=$db->num_rows($this->rows);
		else $this->count=$query[0];
	}

	/* order list with query */
	function order($order,$cols) {

		$order_by=array();
		$clmns=$cols;

		if ($order) {

			$field=split("\^",$order);
			$this->current = $field[0];
			$fields=split("\|",$field[1]);

			for ($i=0;$i<count($fields);$i++) {

				$priority=null;

				if (ereg("^$field[0](\+|\ )$",$fields[$i])) {

					$fields[$i]=$field[0]."-";
				} elseif (ereg("^$field[0]\-$",$fields[$i])) {

					$fields[$i]=$field[0];
				} elseif (ereg("^$field[0]$",$fields[$i])) {
 
					$fields[$i]=$field[0]."+";
				}

				if (ereg("(\+|\ )$",$fields[$i])) {

					$field_name=ereg_replace
							("(\+|\ )","",$fields[$i]);
					$cols[$field_name]="ASC";

					if ($field_name != $field[0])

						array_push($order_by,
							" $field_name ASC");
					else $priority="$field_name ASC";
				} elseif (ereg("\-$",$fields[$i])) {

					$field_name=ereg_replace
							("\-","",$fields[$i]);
					$cols[$field_name]="DESC";

					if ($field_name != $field[0])

						array_push($order_by,
							" $field_name DESC");
					else $priority=" $field_name DESC";
				} else {

					$cols[$fields[$i]]="DEFAULT";
				}

				if ($priority)

					array_unshift($order_by,$priority);
			}

			$this->order=join("|",$fields);
			$this->cols=$cols;

			if (count($order_by)>0)

				$this->order_by=" ORDER BY ".
						join(",",$order_by);
		} else {

			$this->order=key($cols);
			next($cols);

			while ($col=key($cols)) {

				$this->order.= "|".$col;
				next($cols);
			}

			$this->cols=$cols;
		}

		while ($col=key($cols)) {

			$c=$col."_order";
			$this->$c=$col."^".$this->order;

			next($cols);
		}
	}

	/* navigate list */
	function scroll($page,$limit,$action) {

		if (!$this->count) $this->page = 0;
		elseif ($page) $this->page=$page;
		if ($limit) $this->limit=$limit;
		if ($action == "next") $this->page++;
		if ($action == "prev" && $this->page>1) $this->page--;
		#print $this->page;
	}

	function get_list($query) {

		$this->offset=($this->page - 1) * $this->limit;

		if ($this->order_by)
				$query=eregi_replace("ORDER BY.*$","",$query);

		#print($query.$this->order_by.
		#		" LIMIT $this->limit OFFSET $this->offset");
		$this->rows=&$this->db->query($query=$query.$this->order_by.
				" LIMIT $this->limit OFFSET $this->offset");

		#print $query;
		if ($this->rows)
			$this->total=$this->db->num_rows($this->rows);
		if ($this->total) $this->pages=$this->count / $this->limit ;
		elseif (!$this->total) $this->pages=0;
		else $this->pages=1;

		//if (!$this->pages) $this->pages=1;

		$pages=split("\.",$this->pages);

		if ($this->pages>$pages[0]) $this->pages=$pages[0]+1;
		#$this->last = $this->pages+1;

		if ($this->page>$this->pages) $this->page=$this->pages;
		if ($this->limit+$this->offset<$this->count)

				$this->next=true;
		if ($this->offset>0) $this->prev=true;
	}
}

?>
